<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
    require_once("class.connectSQL.inc");
    
    function converteData($data){
       if (strstr($data, "/")){//verifica se tem a barra /
           $d = explode ("/", $data);//tira a barra
           $rstData = "$d[2]-$d[1]-$d[0]";//separa as datas $d[2] = ano $d[1] = mes etc...
           return $rstData;
       }
       else if(strstr($data, "-")){
          $data = substr($data, 0, 10);
          $d = explode ("-", $data);
          $rstData = "$d[2]/$d[1]/$d[0]";
          return $rstData;
       }
       else{
           return '';
      }
}   
   
    
    $banco = new connectSQL();
	$banco1 = new connectSQL();
	$banco2 = new connectSQL();
	$banco3 = new connectSQL();
	$banco4 = new connectSQL();
	$banco5 = new connectSQL();
    if(isset($_POST["acao"])){
		
		if($_POST["acao"]=="I"){ 
			$dados = array(
							"nome"  => $_POST["nome"],
							"data"  => converteData($_POST["data"]),
							"desc"  => $_POST["texto"]
							);
			
			$banco->Insert("evento", $dados);
			//die($_POST["acao"]);
			$msg = "Incluido com sucesso";
		}
		if($_POST["acao"]=="A"){ 
		 $sql = "	UPDATE  `evento` SET  `nome` =  '".$_POST["nome"]."',
`data` =  '".converteData($_POST["data"])."',
`desc` =  'asdasdqwe' WHERE  `idevento` = ".$_POST["ideventos"];
                    //die($sql);
			
			$banco3->Generico($sql);
			//die($_POST["acao"]);
			$msg = "Alterado com sucesso";
		}
		
		}
		
		if(isset($_GET["excluir"])){
				$sql = "Delete from evento where idevento= ".$_GET["cod"]." ";
				//die($sql);
				
				$banco3->Generico($sql);
				//die($_POST["acao"]);
				$msg = "Excluido com sucesso";
			}
		
		
?>

	<head>
	<?php include "head.php"; ?>
 <script>   
    function validar(form){
    if(form.marca.value == ''){
        alert("O campo Marca é obrigatório.");
        return false;
    }
   
    return true;
	}
  </script>
	</head>
	<body>
    	<!-- Header -->
        <div id="header">
            <?php include "sair.php"; ?>
            
            <!-- Header. Main part -->
            <div id="header-main">
                <div class="container_12">
                    <div class="grid_12">
                        <div id="logo">
                            <ul id="nav">
                                <li id="current"><a href="">Eventos</a></li>
                                
                            </ul>
                        </div><!-- End. #Logo -->
                    </div><!-- End. .grid_12-->
                    <div style="clear: both;"></div>
                </div><!-- End. .container_12 -->
            </div> <!-- End #header-main -->
            <div style="clear: both;"></div>
            <!-- Sub navigation -->
          
        </div> <!-- End #header -->
        
		<div class="container_12" align="center" >
        

            <div class="module">
                     <h2><span>Cadastro de Eventos</span></h2>
                        
                     <div class="module-body">
                         <form action="cadeventos.php"  onsubmit="return validar(this);" id="form" method="post">
                        
                            <p align="left">
                            
                            
                                
                                <input type="hidden" name="ideventos" id="ideventos" value="<?php  if(isset($_GET["altera"])){echo $_GET["cod"];} ?>" />
                                
                            </p>
                            
                            <?php
					 
					 	 	 if(isset($_GET["altera"])){
							 $resultado = $banco4->Query("SELECT * FROM evento where idevento = '".$_GET["cod"]."'");
							 foreach ($resultado as $resut){
								$titulo =  $resut['nome'];
								$data = converteData($resut['data']);
								$texto =  $resut['desc'];
							 }
                                                        }else{
                                                                       $titulo = "";
                                                                       $data = "";
                                                                       $texto = "";
                                                         } 
						 
				?>
                            
                            <p align="left">
                                <label>Título</label>
                                <input type="text" class="input-medium" id="nome" name="nome" value="<?php echo $titulo; ?>" />
                                
                            </p>
                             <p align="left">
                                <label>Data</label>
                                <input type="text" class="input-medium" id="data" name="data" value="<?php echo $data; ?>" />
                                
                            </p>
                            <p align="left">
                                <label>Descriçao</label>
                                <textarea cols="50" rows="40" name="texto"><?php echo $texto; ?></textarea>
                            </p>
                            
                            
                           <input type="hidden" name="acao" value="<?php if(isset($_GET["altera"])){echo "A";}else{echo "I";} ?>" />
                           
                            <fieldset>
                                <input class="submit-green" type="submit" value="Salvar" /> 
                                <input class="submit-gray"  type="reset" value="Cancelar" />
                            </fieldset>
                        </form>
                     </div> 
                      <!-- End .module-body -->
					 
                </div>  <!-- End .module -->
				<?php 
				if(isset($msg)){
				echo "<span class='notification n-success'>".$msg."</span>";
				}
				?>
				
				<?php
					 $resultado = $banco->Query("SELECT * FROM evento ");
				?>
                     <div class="module">
                	<h2>Eventos</h2>
                    
                    <div class="module-table-body">
                    	<form action="">
                        <table id="myTable" class="tablesorter">
                        	
                                <tr>
                                    
                                    <th style="width:20%">Nome</th>
                                    <th style="width:20%">Data</th>
                                    <th style="width:15%"></th>
                                </tr>
                            
                            <tbody>
                            <?php
                            
    
        foreach ($resultado as $resut){
        echo "<tr>";
		echo "<td>". $resut['nome']."</td>";
		echo "<td>". converteData($resut['data'])."</td>";
                
		?>
        <td><a href="cadeventos.php?altera=1&cod=<?php echo $resut['idevento']; ?>"><img src="pencil.gif" tppabs="http://www.xooom.pl/work/magicadmin/images/pencil.gif" width="30" height="30" alt="edit" /></a>
        <?php ?>
        <a href="cadeventos.php?excluir=1&cod=<?php echo $resut['idevento']; ?>"><img src="bin.gif" tppabs="http://www.xooom.pl/work/magicadmin/images/bin.gif" width="30" height="30" alt="delete" /></a>
        
        <a href="#" onclick="window.open('fotoevento.php?cod=<?php echo $resut['idevento']; ?>', 'Foto', 'STATUS=NO, TOOLBAR=NO, LOCATION=NO, DIRECTORIES=NO, RESISABLE=NO, SCROLLBARS=YES, TOP=10, LEFT=10, WIDTH=770, HEIGHT=400');"><img src="foto.png" tppabs="http://www.xooom.pl/work/magicadmin/images/bin.gif" width="30" height="30" alt="delete" /></a>    
        <?php  
		
			
  		echo "</tr>";
		}
   		                  ?>  
                                
                                             
                     
                     
                      </tbody>
                        </table>
                        </form>
                     
                     
                 </div>    
                     
                     
            <div style="clear:both;"></div>
            
            <div style="clear:both;"></div>
        </div> 
        
                <!-- Example table -->
                <!-- End .container_12 -->
		
         
        <?php include "footer.php"; ?>
        
	</body>
</html>